package cn.edu.hit.hec;

import cn.edu.hit.kg.data.GraphData;
import cn.edu.hit.kg.entity.BaseNode;
import cn.edu.hit.kg.entity.Component;
import cn.edu.hit.kg.entity.Fault;
import cn.edu.hit.kg.entity.Signal;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by Wang on 2018/12/18.
 */
public class AppTest {
    static Logger logger = LoggerFactory.getLogger(AppTest.class);

    public static void main(String[] args) throws InterruptedException {
        Random r = new Random();
        Map component = Component.getByName("发电机");

//        int[] xs = {51};
//        int[] ys = {60};


        final String unitId="test";
//        for (int i = 0; i < xs.length; i++) {
            long timestamp = System.currentTimeMillis();
            int x = 81;//r.nextInt(100);
            int y = 51;//r.nextInt(100);
            int z = 70;

            List<Signal> rawFeatures = new ArrayList<>();
            Signal signalX = new Signal();
            signalX.setFeatureType("X");
            signalX.setValue(x);
            signalX.setComponent(component);
            rawFeatures.add(signalX);

            Signal signalY = new Signal();
            signalY.setFeatureType("Y");
            signalY.setValue(y);
            signalY.setComponent(component);
            rawFeatures.add(signalY);

            Signal signalZ = new Signal();
            signalZ.setFeatureType("Z");
            signalZ.setValue(z);
            signalZ.setComponent(component);
            rawFeatures.add(signalZ);

            System.out.println(rawFeatures);

            GraphData graphData = GraphData.getInstance();
//                        List<Map> results =
            Map<Boolean, List<Fault>> finishedFaults =
                    graphData.getIdSetByLabel("Fault").stream().map(Fault::loadById)
                            .peek(fault -> fault.matchSignal(rawFeatures, timestamp, unitId))
                            .filter(fault -> fault.isMatched())

                            .collect(Collectors.groupingBy(f -> f.isFinished(unitId)));

//                        System.out.println(finishedFaults);

            List<Map> results = null;
            if (finishedFaults.get(false) != null && finishedFaults.get(false).size() > 0) {
                results = finishedFaults.get(false).stream()
                        .map(fault -> fault.transferFaultToMap(unitId))
                        .peek(map -> {
                            map.put("timestamp", timestamp);
                            if ((long) map.get("last_timestamp") == timestamp) {
                                map.put("last_timestamp", 0);
                            }
                        })
                        .collect(Collectors.toList());
            }

            List<String> finishedIds = null;
            if (finishedFaults.get(true) != null && finishedFaults.get(true).size() > 0) {
                finishedIds = finishedFaults.get(true).stream()
                        .map(f -> f.getReportId(unitId))
                        .collect(Collectors.toList());
            }

            JSONObject jsonObject = new JSONObject();

            jsonObject.put("timestamp", timestamp);
            jsonObject.put("new_data", results);
            jsonObject.put("end_data", finishedIds);

            Map nodes = new HashMap<>();
            if (results != null) {
                results.stream()
                        .map(map -> (long) map.get("id"))
                        .map(BaseNode::loadNodeById)
                        .forEach(node -> nodes.put("" + node.getId(), node.toJson()));

                results.stream()
                        .map(map -> (long) map.get("id"))
                        .flatMap(BaseNode::getAllSubNodes)
                        .map(BaseNode::loadNodeById)
                        .forEach(node -> nodes.put("" + node.getId(), node.toJson()));
            }
            jsonObject.put("node", nodes);
            logger.info("发送数据：{}", jsonObject.toString());

            Thread.sleep(1000);
        }
//    }
}
